//
//  DetailViewController.m
//  UI_元旦豆瓣电影
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
#import "LjcDataBaseHandle.h"
#define W self.view.frame.size.width
#define H self.view.frame.size.height
@interface DetailViewController ()
@property (nonatomic, retain) UIScrollView *scroll;

// Detail
@property (nonatomic, retain) UIImageView *PicImgV;
@property (nonatomic, retain) UILabel *titleL;
@property (nonatomic, retain) UIImageView *timeImgV;
@property (nonatomic, retain) UILabel *timeL;
@property (nonatomic, retain) UIImageView *nameImgV;
@property (nonatomic, retain) UILabel *nameL;
@property (nonatomic, retain) UIImageView *categroyImgV;
@property (nonatomic, retain) UILabel *categroyL;
@property (nonatomic, retain) UIImageView *addressImgV;
@property (nonatomic, retain) UILabel *addressL;
@property (nonatomic, retain) UILabel *contentL;


@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) NSString *str;
@end

@implementation DetailViewController

- (void)dealloc {
    [_model release];
    [_scroll release];
    [_PicImgV release];
    [_titleL release];
    [_timeImgV release];
    [_timeL release];
    [_nameImgV release];
    [_nameL release];
    [_categroyImgV release];
    [_categroyL release];
    [_addressImgV release];
    [_addressL release];
    [_contentL release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    self.rect = [_model.content boundingRectWithSize:CGSizeMake(335, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    [self navHandle];
    [self scrollHandle];
    [self DetailHandle];
    [self button];
    [[LjcDataBaseHandle sharedDataBaseHandle] createActivityTable:@"Activity"];
}


- (void)button {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(iAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    self.str = @"是";
}

- (void)iAction:(UIBarButtonItem *)item {
    if ([_str isEqualToString:@"是"]) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"收藏" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:cancel];
        UIAlertAction *OK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _str = _model.title;
          
            
        }];
        [alert addAction:OK];
    } else {
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"收藏" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alter animated:YES completion:^{
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alter addAction:cancel];
    }
    
    [self shoucang];
    
    
}

- (void)shoucang {
    
    [[LjcDataBaseHandle sharedDataBaseHandle] insertTable:_model];
    
}

- (void)DetailHandle {
    self.titleL = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 300, 40)];
    _titleL.backgroundColor = [UIColor clearColor];
    _titleL.font = [UIFont systemFontOfSize:25];
    _titleL.text = _model.title;
    [self.scroll addSubview:_titleL];
    [_titleL release];
    
    self.PicImgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, _titleL.frame.origin.y + 60, 90, 150)];
    _PicImgV.backgroundColor = [UIColor clearColor];
    [_PicImgV sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    [self.scroll addSubview:_PicImgV];
    [_PicImgV release];
    
    self.timeImgV = [[UIImageView alloc] initWithFrame:CGRectMake(_PicImgV.frame.origin.x + 100, _PicImgV.frame.origin.y, 20, 20)];
    _timeImgV.backgroundColor = [UIColor clearColor];
    _timeImgV.image = [UIImage imageNamed:@"icon_date_blue.png"];
    [self.scroll addSubview:_timeImgV];
    [_timeImgV release];
    
    self.timeL = [[UILabel alloc] initWithFrame:CGRectMake(_timeImgV.frame.origin.x + 30, _timeImgV.frame.origin.y, 200, 20)];
    _timeL.backgroundColor = [UIColor clearColor];
    NSString *str = [_model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str1 = [_model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *str2 = [NSString stringWithFormat:@"%@ -- %@", str, str1];
    _timeL.font = [UIFont systemFontOfSize:15];
    _timeL.text = str2;
    [self.scroll addSubview:_timeL];
    [_timeL release];
    
    self.nameImgV = [[UIImageView alloc] initWithFrame:CGRectMake(_timeImgV.frame.origin.x, _timeL.frame.origin.y + 40, 20, 20)];
    _nameImgV.backgroundColor = [UIColor clearColor];
    _nameImgV.image = [UIImage imageNamed:@"icon_sponsor_blue.png"];
    [self.scroll addSubview:_nameImgV];
    [_nameImgV release];
    
    self.nameL = [[UILabel alloc] initWithFrame:CGRectMake(_timeL.frame.origin.x, _nameImgV.frame.origin.y, 200, 20)];
    _nameL.backgroundColor = [UIColor clearColor];
    _nameL.text = _model.name;
    [self.scroll addSubview:_nameL];
    [_nameL release];
    
    self.categroyImgV = [[UIImageView alloc] initWithFrame:CGRectMake(_nameImgV.frame.origin.x, _nameL.frame.origin.y + 40, 20, 20)];
    _categroyImgV.backgroundColor = [UIColor clearColor];
    _categroyImgV.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    [self.scroll addSubview:_categroyImgV];
    [_categroyImgV release];
    
    self.categroyL = [[UILabel alloc] initWithFrame:CGRectMake(_nameL.frame.origin.x, _categroyImgV.frame.origin.y, 200, 20)];
    _categroyL.backgroundColor = [UIColor clearColor];
    _categroyL.text = _model.category_name;
    [self.scroll addSubview:_categroyL];
    [_categroyL release];
    
    self.addressImgV = [[UIImageView alloc] initWithFrame:CGRectMake(_categroyImgV.frame.origin.x, _categroyImgV.frame.origin.y + 40, 20, 20)];
    _addressImgV.backgroundColor = [UIColor clearColor];
    _addressImgV.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    [self.scroll addSubview:_addressImgV];
    [_addressImgV release];
    
    self.addressL = [[UILabel alloc] initWithFrame:CGRectMake(_categroyL.frame.origin.x, _addressImgV.frame.origin.y, 200, 40)];
    _addressL.backgroundColor = [UIColor clearColor];
    _addressL.font = [UIFont systemFontOfSize:15];
    _addressL.lineBreakMode = NSLineBreakByCharWrapping;
    _addressL.text = _model.address;
    _addressL.numberOfLines = 2;
    [self.scroll addSubview:_addressL];
    [_addressL release];
    
    self.contentL = [[UILabel alloc] initWithFrame:CGRectMake(_PicImgV.frame.origin.x, _PicImgV.frame.origin.y + 250, _rect.size.width, _rect.size.height)];
    _contentL.backgroundColor = [UIColor clearColor];
    _contentL.text = _model.content;
//    _contentL.font = [UIFont systemFontOfSize:17];
    _contentL.numberOfLines = 100;
    _contentL.lineBreakMode = NSLineBreakByTruncatingHead;
    
//    NSLog(@"%@", _model.content);
    [self.scroll addSubview:_contentL];
    [_contentL release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_contentL.frame.origin.x, _contentL.frame.origin.y - 70, 200, 50)];
    label.backgroundColor = [UIColor clearColor];
    [self.scroll addSubview:label];
    label.text = @"活动介绍";
    label.font = [UIFont systemFontOfSize:25];
    [label release];
    
    
}

- (void)scrollHandle {
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll];
    [_scroll release];
    
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width, 500 + _rect.size.height);
    
    _scroll.bounces = NO;
}

- (void)navHandle {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 green:0.4 blue:0.5 alpha:1];
    self.navigationItem.title = self.model.title;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(itAction:)];
    self.navigationItem.leftBarButtonItem = item;
    [item release];
}

- (void)itAction:(UIButton *)bu {
    [self.navigationController popViewControllerAnimated:YES];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
